package com.tianya.portal.blog.controller;

import com.tianya.common.web.core.domain.AjaxResult;
import com.tianya.common.web.core.page.TableDataInfo;
import com.tianya.portal.blog.domain.BlogNoteBook;
import com.tianya.portal.blog.domain.BlogStatistics;
import com.tianya.portal.blog.domain.vo.BlogNoteBookVo;
import com.tianya.portal.blog.service.IBlogNoteBookService;
import com.tianya.portal.common.core.controller.BaseController;
import com.tianya.portal.common.utils.MarkDownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客笔记Controller
 *
 * @author tianya
 * @date 2023-04-30
 */
@RestController
@RequestMapping("/noteBook")
public class BlogNoteBookController extends BaseController {
    @Autowired
    private IBlogNoteBookService blogNoteBookService;

    /**
     * 查询博客笔记列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BlogNoteBook blogNoteBook) {
        startPage();
        List<BlogNoteBookVo> list = blogNoteBookService.selectBlogNoteBookList(blogNoteBook);
        return getDataTable(list);
    }

    /**
     * 获取笔记统计
     *
     * @return
     */
    @GetMapping("/getNoteStatistics")
    public AjaxResult getNoteStatistics() {
        BlogStatistics blogStatistics = blogNoteBookService.getBlogStatistics();
        return AjaxResult.success(blogStatistics);
    }

    /**
     * 获取博客笔记详细信息
     */
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id, String password) {
        BlogNoteBook noteBook = blogNoteBookService.selectBlogNoteBookById(id, password);
        noteBook.setPassword("******");
        //noteBook.setContent(MarkDownUtils.getRendererHtml(noteBook.getContent()));
        return success(noteBook);
    }

    /**
     * 获取博客笔记详细信息
     */
    @GetMapping(value = "/html/{id}")
    public String getHtml(@PathVariable("id") Long id, String password) {
        BlogNoteBook noteBook = blogNoteBookService.selectBlogNoteBookById(id, password);
        return MarkDownUtils.getRendererHtml(noteBook.getContent());
    }
}
